﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.templates;
using l1cs.server.datatables;
using l1cs.server.serverpackets;
using l1cs.server.model.skill;

namespace l1cs.server.model.Instance
{
    class L1FieldObjectInstance : L1NpcInstance
    {
        private static Logger _log = Logger.getLogger( typeof( L1FieldObjectInstance ).FullName );

        public L1FieldObjectInstance( L1Npc template )
            : base( template )
        { }

        override public void onAction( L1PcInstance pc )
        {
            if ( getNpcTemplate().get_npcId() == 81171 )
            { // おばけ屋敷のゴールの炎
                if ( L1HauntedHouse.getInstance().getHauntedHouseStatus() ==
                        L1HauntedHouse.STATUS_PLAYING )
                {
                    int winnersCount = L1HauntedHouse.getInstance().
                            getWinnersCount();
                    int goalCount = L1HauntedHouse.getInstance().getGoalCount();
                    if ( winnersCount == goalCount + 1 )
                    {
                        L1ItemInstance item = ItemTable.getInstance()
                                .createItem( 49280 ); // 勇者のパンプキン袋(銅)
                        int count = 1;
                        if ( item != null )
                        {
                            if ( pc.getInventory().checkAddItem( item, count ) ==
                                    L1Inventory.OK )
                            {
                                item.setCount( count );
                                pc.getInventory().storeItem( item );
                                pc.sendPackets( new S_ServerMessage( 403, item.getLogName() ) ); // %0を手に入れました。
                            }
                        }
                        L1HauntedHouse.getInstance().endHauntedHouse();
                    }
                    else if ( winnersCount > goalCount + 1 )
                    {
                        L1HauntedHouse.getInstance().setGoalCount( goalCount + 1 );
                        L1HauntedHouse.getInstance().removeMember( pc );
                        L1ItemInstance item = null;
                        if ( winnersCount == 3 )
                        {
                            if ( goalCount == 1 )
                            {
                                item = ItemTable.getInstance()
                                        .createItem( 49278 ); // 勇者のパンプキン袋(金)
                            }
                            else if ( goalCount == 2 )
                            {
                                item = ItemTable.getInstance()
                                        .createItem( 49279 ); // 勇者のパンプキン袋(銀)
                            }
                        }
                        else if ( winnersCount == 2 )
                        {
                            item = ItemTable.getInstance()
                                    .createItem( 49279 ); // 勇者のパンプキン袋(銀)
                        }
                        int count = 1;
                        if ( item != null )
                        {
                            if ( pc.getInventory().checkAddItem( item, count ) ==
                                    L1Inventory.OK )
                            {
                                item.setCount( count );
                                pc.getInventory().storeItem( item );
                                pc.sendPackets( new S_ServerMessage( 403, item
                                        .getLogName() ) ); // %0を手に入れました。
                            }
                        }
                        L1SkillUse l1skilluse = new L1SkillUse();
                        l1skilluse.handleCommands( pc,
                                L1SkillId.CANCELLATION, pc.getId(), pc.getX(),
                                pc.getY(), null, 0, L1SkillUse.TYPE_LOGIN );
                        L1Teleport.teleport( pc, 32624, 32813, (short)4, 5, true );
                    }
                }
            }
        }

        override public void deleteMe()
        {
            _destroyed = true;
            if ( getInventory() != null )
            {
                getInventory().clearItems();
            }
            L1World.getInstance().removeVisibleObject( this );
            L1World.getInstance().removeObject( this );
            foreach ( L1PcInstance pc in L1World.getInstance().getRecognizePlayer( this ) )
            {
                pc.removeKnownObject( this );
                pc.sendPackets( new S_RemoveObject( this ) );
            }
            removeAllKnownObjects();
        }
    }
}
